<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>

    body {
      width: 1000px;
      height: 1000px;
    }

    #box {
      width: 100px;
      height: 100px;
      background-color: red;
      position: absolute;
    }
  </style>

  <script type="text/javascript">

    window.onload = function() {
      document.onmousemove = function(event) {
        event = event || window.event;

        // 兼容处理，部分浏览器认为滚动是body的，部分浏览器认为滚动条是html的  document.documentElement => html
        var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
        var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;

        // clientX 和 clientY 用于获取鼠标在当前可视窗口的坐标 
        var left = event.clientX + scrollLeft;
        var top = event.clientY + scrollTop;
      
        box.style.left = left + "px";
        box.style.top = top + "px";

        // pageX 和 pageY 用于获取鼠标在当前页面的坐标（不兼容IE8及以下）
        // box.style.left = event.pageX + "px";
        // box.style.top = event.pageY + "px";

      };
    };
  </script>
</head>
<body>
  <div id="box"></div>
</body>
</html>